package com.magist.manage.system.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.manage.system.entity.OffTag;
import com.magist.manage.system.service.OffTagService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import com.magist.manage.system.entity.OffCase;
import com.magist.manage.system.dto.OffCaseDto;
import com.magist.manage.system.service.OffCaseService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "RestOffCaseController", description = "RestOffCaseController接口")
@RestController
@RequestMapping("/rest/offCase")
public class RestOffCaseController extends BaseController {

	@Autowired
	private OffCaseService offCaseService;
	@Autowired
	private OffTagService offTagService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@RequestBody OffCaseDto offCaseDto) {
		OffCase offCase = new OffCase();
		BeanUtils.copyProperties(offCaseDto,offCase);
		// list改成#隔开的
		List<String> tags = offCaseDto.getTags();
		if (!CollectionUtils.isEmpty(tags)) {
			offCase.setTags(String.join("#",tags));
		}
		offCase.setId(offCaseService.getNewId());
		offCaseService.insert(offCase);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@RequestBody OffCaseDto offCaseDto) {
		OffCase oldOffCase = offCaseService.getById(offCaseDto.getId());
		if(oldOffCase==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		OffCase offCase = new OffCase();
		BeanUtils.copyProperties(offCaseDto,offCase);
		// list改成#隔开的
		List<String> tags = offCaseDto.getTags();
		if (!CollectionUtils.isEmpty(tags)) {
			offCase.setTags(String.join("#",tags));
		}
		offCase.setModifyTime(oldOffCase.getModifyTime());
		if (offCaseService.update(offCase)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("offCaseDto") OffCaseDto offCaseDto) {
		ResultMessage result = ResultMessage.success();
		OffCase offCase = new OffCase();
		BeanUtils.copyProperties(offCaseDto,offCase);
		String caseName = offCase.getCaseName();
		String caseCode = offCase.getCaseCode();
		if(!StringUtils.isEmpty(caseName)) {
			offCase.setCaseName("%"+caseName+"%");
		}

		if(!StringUtils.isEmpty(caseCode)) {
			offCase.setCaseCode("%"+caseCode+"%");
		}
		PageInfo<OffCase> pageInfo = offCaseService.page(offCase,getPageInfo());
		List<OffTag> offTags = offTagService.find(new OffTag());
		pageInfo.getList().forEach(oCase->{
			String tags = oCase.getTags();
			if(!StringUtils.isEmpty(tags)) {
				String[] split = tags.split("#");
                List<String> nameList = Lists.newArrayList();
				for (String s : split) {
					OffTag offTag = offTagService.findByCode(s);
					if(!ObjectUtils.isEmpty(offTag)) {
                        nameList.add(offTag.getTagName());
                    }
				}
                oCase.setTags(String.join("#",nameList));
            }
		});
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		OffCase offCase = offCaseService.getById(id);
		if(offCase==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(offCaseService.delete(offCase)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		OffCase offCase = offCaseService.getById(id);
		if(offCase==null)
			throw new BusinessException("无效的数据!");
		OffCaseDto offCaseDto = new OffCaseDto();
		BeanUtils.copyProperties(offCase,offCaseDto);
		// tags转换
		String tags = offCase.getTags();
		if(!StringUtils.isEmpty(tags)) {
			offCaseDto.setTags(Arrays.asList(tags.split("#")));
		} else {
			offCaseDto.setTags(Lists.newArrayList());
		}
		ResultMessage result = ResultMessage.success();
		result.setContent(offCaseDto);
		return result;
	}


}
